package mythesis.gui.popup;
import mythesis.gui.popup.controller.*;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.input.*;
import javafx.ext.swing.*;
import javafx.scene.control.*;
import javafx.scene.effect.*;
import javafx.scene.text.*;
import java.lang.String;

public class InputPopup extends CustomNode
{
    var maxInput   = 12;				// Max allowed constant input (including values and varnames)
   	public var toClose   = false;		// determines if close is clicked
    public var toAccept  = false;	// determines if accept is clicked
    public var toNotify  = "";		// determines if InputPopup wants to notify user
    public var textInput = bind {inputs.content[0] as TextBox}.rawText;	// constant input
    
    public var selectedDataValue = bind varOrConst.selectedToggle.value;
    public var selectedType = bind {inputs2.content[0] as ChoiceBox}.selectedItem;
    public var selectedVariable = bind {inputs.content[1] as ChoiceBox}.selectedItem.toString();
    public var selectedSpecial = bind {inputs.content[2] as ChoiceBox}.selectedItem.toString();
    
    public var allowedDataTypes:Boolean[] = [false, false, false, false];	// set this to determine allowed data types (bool, string, int, spec)
    public var allowedDataValue:Boolean[] = [false, false, false, false];  // set this to determine allowed data values (constant, variable, special)
    public var userVariables_Number:String[] = [null];				// set this to list of user variables from main.
    public var userVariables_Word:String[]   = [null];
    public var userVariables_Truth:String[]  = [null];
    public var specialVariables:String[] = [null];
    //public var specialVariables_Number:String[]  = [null];
    //public var specialVariables_Special:String[]  = [null];
    //public var specialVariables_Special:String[]  = [null];
    //public var specialVariables_Special:String[]  = [null];
    var varOrConst = ToggleGroup{}; 
    
    
    //var radios = SwingToggleGroup{};
    
	var rbConstant = RadioButton
	{
	    disable     : bind not allowConstant
		translateX  : 25
		translateY  : 50
		toggleGroup : varOrConst
		value       : 1 
	}
	var rbVariable = RadioButton
	{
	    disable     : bind not allowVariable
		translateX  : 25
		translateY  : 90   
		toggleGroup : varOrConst
		value       : 2
	}
	var rbSpecial = RadioButton
	{
	    disable     : bind not allowSpecial
		translateX  : 25
		translateY  : 130
		toggleGroup : varOrConst 
		value       : 3
	}

    
    var fillNormal = "#009EEA";
    var fillHover  = "#79D3FF";
    
    var closeNormal = "#990000";
    var closeHover  = "#FF0000";

    var acceptFillNormal = "#000066";
    var acceptFillHover  = "#00009D";
    
    var rectFill1  = fillNormal;
    var rectFill2  = fillNormal;
    var rectFill3  = fillNormal;
    var closeFill  = closeNormal;
    var acceptFill = acceptFillNormal; 

     
    
    var listOfAllowedTypes:String[] = ["Something went wrong"];
    var listOfVariableNames:String[] = [];
    var listOfSpecialOptions:String[] = []; //[">", ">=", "==", "<=", "<", "!="];
    var allowConstant = false;
    var allowVariable = false;
    var allowSpecial  = false;
   
    function verifyInput():Boolean
    {
        // check if constant input is TOO LONG
        if (textInput.length() >= maxInput){
            var excess = textInput.length() - maxInput;
            toNotify = "Input too long!  {excess} character(s) above the limit of {maxInput}!";
            return false;
        }
        
        // check if NO DATA TYPE is selected, if THERE ARE PICKABLE TYPES
        if (listOfAllowedTypes.size() > 0 and (selectedType == "" or selectedType == null))
        {
            toNotify = "Pick a data type, please!";
            return false;
        }
        // check if NO DATA VALUE is selected
        if (selectedDataValue == -1 or selectedDataValue == "" or selectedDataValue == null)
        {
            toNotify = "Pick what kind of input you want.";
            return false;
        }
        
        
        // check if VARIABLE DATA VALUE is selected, but NO VARIABLE SELECTED
        if (selectedDataValue == 2)
        {
        	if (selectedVariable == "" or selectedVariable == null)
        	{
        	    toNotify = "You forgot to pick a variable.";
        	    return false;
        	}
        }
        // check if input is NOT BLANK if data value is CONSTANT
        if (selectedDataValue == 1 and (textInput == "" or textInput == null))
        {
            toNotify = "You forgot to type an input.";
            return false;
        }
              
        // check if input is NUMBERS ONLY if data value is CONSTANT 
        if (selectedDataValue == 1 and selectedType == "Number")
        {
            if (VerifyInput.numOnly(textInput) == false)
            {
				toNotify = "Type only numbers please!";
				return false;                
            }
            else
            {
                return true;
            }
        }
        
        // check if input is NUMBERS ONLY if data value is CONSTANT 
        if (selectedDataValue == 1 and selectedType == "Word")
        {
            if (VerifyInput.lettersOnly(textInput) == false)
            {
				toNotify = "Type only letters please!  No spaces, numbers or any special characters!  Only A-Z, a-z!";
				return false;                
            }
            else
            {
                return true;
            }
        }
        
        // check if input is TRUE or FALSE ONLY if data value is CONSTANT
        if (selectedDataValue == 1 and selectedType == "Truth")
        {
            if (VerifyInput.boolOnly(textInput) == false)
            {
                toNotify = "Should either be true or false only!";
                return false;
            }
            else
            {
                return true;
            };
        }

        
        return true;
    } 
   
    public function updateLists()
    {
        // user variables
        //listOfVariableNames = [];
        
        if (listOfVariableNames.size() == 0)
        //if (selectedVariable != "")
        {
        	if ({inputs2.content[0] as ChoiceBox}.selectedItem == "Number")
        	{
	            listOfVariableNames = userVariables_Number ;
        	}
        	else if ({inputs2.content[0] as ChoiceBox}.selectedItem == "Word")
        	{
	            listOfVariableNames = userVariables_Word ;
        	}
        	else if ({inputs2.content[0] as ChoiceBox}.selectedItem == "Truth")
        	{
	            listOfVariableNames = userVariables_Truth ;
        	}
        }
        if (listOfSpecialOptions.size() == 0)
        {
			//if ({inputs2.content[0] as ChoiceBox}.selectedItem == "Special")
        	//{
	            listOfSpecialOptions = specialVariables ;
        	//}
        }
    }
    
    public function updateAllowables()
    {
        // data types
        listOfAllowedTypes = [];
        if (allowedDataTypes[0] == true)
        {
            insert "Truth" into listOfAllowedTypes;
        }
        if (allowedDataTypes[1] == true)
        {
            insert "Word" into listOfAllowedTypes;
        }
        if (allowedDataTypes[2] == true)
        {
            insert "Number" into listOfAllowedTypes;
        }
        if (allowedDataTypes[3] == true)
        {
            insert "Special" into listOfAllowedTypes;
        }
        
        // data value
        allowConstant = allowedDataValue[0];
        allowVariable = allowedDataValue[1];
        allowSpecial  = allowedDataValue[2];
        
    }
    
    public function initSettings(type:Number, dataValue:Number, text:String)
    {        
    	//println(allowedDataTypes);
        // Initialize text display
        if (dataValue==1){	{inputs.content[0] as TextBox}.text = text;	}
        // Select variable, but forget it for now.
        else if (dataValue==2)
        {
            /*
        	for (i in [0..{inputs.content[1] as ChoiceBox}.items.size()])
        	{
        	    //println("{{inputs.content[1] as ChoiceBox}.items[i]} =?= {text}");
        	    if ({inputs.content[1] as ChoiceBox}.items[i] == text)
        	    {
        	        println("yeah!");
        	        {inputs.content[1] as ChoiceBox}.select(i);
        	        break;
        	    }
        	}
        	*/   
        }
        
        if (dataValue==1)		{	rbConstant.selected = true; }
        else if (dataValue==2)	{	rbVariable.selected = true; }
        else if (dataValue==3)	{	rbSpecial.selected  = true; }
        
        // Select current data type.
        // 
        if (type==1)
        {
        	{inputs2.content[0] as ChoiceBox}.select(0);
        }
        else if (type==3)
        {
			{inputs2.content[0] as ChoiceBox}.select(0);
        }
        
        // Update lists
        updateLists();
        
    }
    
    public var inputs2:Group = Group
    {
        content: bind
        [
			ChoiceBox
			{
			    translateX : 50
			    translateY : 10
			    width      : 125
			    items      : bind listOfAllowedTypes;
			    onMouseExited: function(e:MouseEvent):Void
			    {
		        	if ({inputs2.content[0] as ChoiceBox}.selectedItem == "Number")
		        	{
			            listOfVariableNames = userVariables_Number ;
		        	}
		        	else if ({inputs2.content[0] as ChoiceBox}.selectedItem == "Word")
		        	{
			            listOfVariableNames = userVariables_Word ;
		        	}
		        	else if ({inputs2.content[0] as ChoiceBox}.selectedItem == "Truth")
		        	{
			            listOfVariableNames = userVariables_Truth ;
		        	}
					if ({inputs2.content[0] as ChoiceBox}.selectedItem == "Special")
		        	{
			            listOfSpecialOptions = specialVariables ;
		        	}
			    }
			    //tooltip    : Tooltip { text: "Select an allowed type." }
			}
        ]
    }
    
    public var inputs = Group
    {
        content: bind
        [
			TextBox
			{
			    disable    : bind not allowConstant
			    translateX : 50
			    translateY : 48
			    columns    : 25
			    text       : ""
			    //tooltip    : Tooltip { text: "Type a constant value.\nLimited to A-Z, a-z and 0-9." }
			    font       : Font.font("Courier New", FontWeight.REGULAR, 11)
			    promptText : "Type literal value"
			    onKeyPressed: function(e:KeyEvent)
			    			  {
			    			      if (e.code == KeyCode.VK_ENTER)
			    			      {
			    			          toAccept = true;
			    			      }
			    			  }
			    
			}
			ChoiceBox
			{
			    disable    : bind not allowVariable
			    translateX : 50
			    translateY : 87
			    width      : 200
			    items      : bind listOfVariableNames 
			    onMouseEntered: function(e:MouseEvent):Void
			    {
			        updateLists();
			    }
			    //tooltip    : Tooltip { text: "Select an available variable." }
			}
			ChoiceBox
			{
			    disable    : bind not allowSpecial
			    translateX : 50
			    translateY : 127
			    width      : 200
			    items      : bind listOfSpecialOptions 
			    onMouseEntered: function(e:MouseEvent):Void
			    {
			        updateLists();
			    }
			    //tooltip    : Tooltip { text: "Select an available special option." }
			}
			rbSpecial,
			rbVariable,
			rbConstant
        ]
    } 
    public var components = Group
    {
        content:
        [
			Circle
			{
			    centerX : 240
			    centerY : 10
			    radius  : 18
			    fill    : Color.WHITE
			    //effect     : DropShadow { offsetY: 4 }
			}
			Circle
			{
			    centerX : 240
			    centerY : 10
			    radius  : 15
			    fill    : bind Color.web(closeFill);
			    onMousePressed : function(e:MouseEvent):Void{ toClose   = true; }
				onMouseEntered : function(e:MouseEvent):Void{ closeFill = closeHover; }
			    onMouseExited  : function(e:MouseEvent):Void{ closeFill = closeNormal; }
			}
			Rectangle
			{
			    translateX : 10
			    translateY : 40
			    height     : 35
			    width      : 230
			    arcWidth   : 5
			    arcHeight  : 5
			    fill       : bind Color.web(rectFill1);
			    onMousePressed : function(e:MouseEvent):Void
			    {
			        
			        if (not rbConstant.disabled){	rbConstant.selected = true; }
			    }
			    onMouseEntered : function(e:MouseEvent):Void{ rectFill1 = fillHover; }
			    onMouseExited  : function(e:MouseEvent):Void{ rectFill1 = fillNormal; }
			}
			Rectangle
			{
			    translateX : 10
			    translateY : 80
			    height     : 35
			    width      : 230
			    arcWidth   : 5
			    arcHeight  : 5
			    fill       : bind Color.web(rectFill2);
				onMouseEntered : function(e:MouseEvent):Void{ rectFill2 = fillHover; }
			    onMousePressed : function(e:MouseEvent):Void
			    {
			        if (not rbVariable.disabled){	rbVariable.selected = true; }
			    }
			    onMouseExited  : function(e:MouseEvent):Void{ rectFill2 = fillNormal; }
			}
			Rectangle
			{
			    translateX : 10
			    translateY : 120
			    height     : 35
			    width      : 230
			    arcWidth   : 5
			    arcHeight  : 5
			    fill       : bind Color.web(rectFill3);
			    onMousePressed : function(e:MouseEvent):Void
			    {
			        if (not rbSpecial.disabled){	rbSpecial.selected = true; }
			    }
				onMouseEntered : function(e:MouseEvent):Void{ rectFill3 = fillHover; }
			    onMouseExited  : function(e:MouseEvent):Void{ rectFill3 = fillNormal; }
			}

			Text
			{
			    translateX : 15
			    translateY : 24
			    content    : "Type:"
			    fill       : Color.WHITE
			}



			Rectangle
			{
			    translateX : 100
			    translateY : 160
			    width      : 50
			    height     : 25
			    arcWidth   : 5
			    arcHeight  : 5
			    fill       : bind Color.web(acceptFill);
			    //text       : "Accept"
				onMouseEntered : function(e:MouseEvent):Void{ acceptFill = acceptFillHover; }
			    onMouseExited  : function(e:MouseEvent):Void{ acceptFill = acceptFillNormal; }			    
			    onMousePressed: function(e:MouseEvent):Void
			    				{
									if (verifyInput()){  toAccept = true;  }			    				    
			    				}
			}
			Text
			{
			    translateX : 110
			    translateY : 177
			    fill       : Color.WHITE
			    content    : "Accept"
			}
			

        ]
    }
    
    var window = Group
    {
        content:
        [
        	Rectangle
        	{
        	    width      : 250
        	    height     : 190
        	    arcWidth   : 10
        	    arcHeight  : 10
        	    fill       : Color.web("#006699")
        	    //effect     : DropShadow {
        	    //    			offsetX: 5
        	    //    			offsetY: 5
        	    //    			radius : 10
        	    //    			blurType: BlurType.ONE_PASS_BOX
        	    //    		}
        	}
        ]
    }
    
    var group = Group
    {
        content:
        [
        	window,
        	components,
        	inputs,
        	inputs2
        ]
    }
    
    override public function create() : Node
    {
        return group;
    }
}